#include "taskItemTable.h"
#include "version.h"
#include "logger.h"
#include "db.h"
#include <string.h>
#include <stdlib.h>

/**
 *  
 */
#define TASK_ITEM_NAME "t_task_item"
#define SQL_TASK_ITEM_CREATE "CREATE TABLE IF NOT EXISTS " TASK_ITEM_NAME " (id INT PRIMARY KEY NOT NULL, taskID INT NOT NULL, deviceID INT NOT NULL, itemID INT NOT NULL)"
#define SQL_TASK_ITEM_INSERT(...) "INSERT INTO " TASK_ITEM_NAME "(id, taskID, deviceID, itemID) VALUES (" #__VA_ARGS__ ")"
#define SQL_TASK_ITEM_REPLACE(...) "REPLACE INTO " TASK_ITEM_NAME "(id, taskID, deviceID, itemID) VALUES (" #__VA_ARGS__ ")"
#define SQL_TASK_ITEM_DELETE(id) "DELETE FROM " TASK_ITEM_NAME " WHERE id = " #id
#define SQL_TASK_ITEM_SELECT(id) "SELECT * FROM " TASK_ITEM_NAME " WHERE id = " #id



/*===============================================================*/
/* 联动 */
int createTaskItemTable(){
    return db_create_table(SQL_TASK_ITEM_CREATE);
}

int insertTaskItem(T_TASK_ITEM* item){
    if(NULL == item){
        log_file_e(APPNAME, "insert task item is NULL");
        return -1;
    }
    int ret = db_insert(SQL_TASK_ITEM_INSERT(item->id, item->taskID, item->deviceID, item->itemID));
    return ret;
}

int replaceTaskItem(T_TASK_ITEM* item){
    if(NULL == item){
        log_file_e(APPNAME, "replace task item is NULL");
        return -1;
    }
    int ret = db_insert(SQL_TASK_ITEM_REPLACE(item->id, item->taskID, item->deviceID, item->itemID));
    return ret;
}

int deleteTaskItem(T_TASK_ITEM* item){
    if(NULL == item){
        log_file_e(APPNAME, "delete task item is NULL");
        return -1;
    }
    return db_delete(SQL_TASK_ITEM_DELETE(item->id));
}
/**
 *  
 */
static int taskItemCallback(void *data, int argc, char **argv, char **azColName){
    int i = 0;
    if(NULL == data){
        log_file_e(APPNAME, "taskItemCallback data is NULL");
        return -1;
    }
    T_TASK_ITEM_LIST* itemList = (T_TASK_ITEM_LIST*)data;
    if((0 == itemList->count) || (NULL == itemList->head)){
        itemList->head = itemList->tail = (T_TASK_ITEM*)malloc(sizeof(T_TASK_ITEM));
        itemList->head->next = NULL;
        itemList->head->prev = NULL;
    } else {
        itemList->tail->next = (T_TASK_ITEM*)malloc(sizeof(T_TASK_ITEM));
        itemList->tail->next->prev = itemList->tail;
        itemList->tail = itemList->tail->next;
    }
    itemList->count++;
    for(i = 0; i < argc; i++){
        if(0 == strcmp(azColName[i], "id")){
            itemList->tail->id = atoll(argv[i]);
        } else if(0 == strcmp(azColName[i], "taskID")){
            itemList->tail->taskID = atoll(argv[i]);
        } else if(0 == strcmp(azColName[i], "deviceID")){
            itemList->tail->deviceID = atoll(argv[i]);
        } else if(0 == strcmp(azColName[i], "itemID")){
            itemList->tail->itemID = atoll(argv[i]);
        }
        log_i("%s = %s", azColName[i], argv[i] ? argv[i] : "NULL");
    }
    return itemList->count;
}

int selectTaskItem(const char* sql, T_TASK_ITEM_LIST* itemList){
    if((NULL == sql) || (1 > strlen(sql)) || (NULL == itemList)){
        log_file_e(APPNAME, "select product sql is NULL");
        return -1;
    }
    return db_select(sql, taskItemCallback, itemList);

}
//
void dbTaskItemInit(){
    createTaskItemTable();
}
